package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询学生列表
     *
     * @param param 查询参数
     * @return 分页查询结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<Student> getPageResult(StudentQueryParam param) {
        Page<Student> page = new Page<>(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));

        page = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Student::getName, param.getName())
                .eq(param.getDegree() != null && param.getDegree() != 0, Student::getDegree, param.getDegree())
                .eq(param.getClazzId() != null && param.getClazzId() != 0, Student::getClazzId, param.getClazzId())
                .page(page);

        List<Student> students = page.getRecords();

        List<Integer> clazzIds = page.getRecords().stream().map(Student::getClazzId).collect(Collectors.toList());

        if(!clazzIds.isEmpty()) {
            List<Clazz> clazzs = clazzService.listByIds(clazzIds);
            students.forEach(student -> {
                clazzs.forEach(clazz -> {
                    if(student.getClazzId() != null && student.getClazzId().equals(clazz.getId())) {
                        student.setClazzName(clazz.getName());
                    }
                });
            });
        }

        return new PageResult<>(page.getTotal(), students);
    }

    /**
     * 新增学生信息
     *
     * @param student 学生信息
     */
    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        save(student);
    }

    /**
     * 根据id查询学生信息
     *
     * @param id 学生id
     * @return 学生信息
     */
    @Override
    public Student getStudentById(Integer id) {
        return getById(id);
    }

    /**
     * 修改学生信息
     *
     * @param student 学生信息
     */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 学生id集合
     */
    @Override
    public void deleteStudentsByIds(List<Integer> ids) {
        removeByIds(ids);
    }

    /**
     * 扣分
     *
     * @param id    学生id
     * @param score 扣分数
     */
    @Override
    public void violation(Integer id, Integer score) {
        Student student = getById(id);
        student.setViolationScore((short) (student.getViolationScore() + score));
        student.setViolationCount((short) (student.getViolationCount() + 1));
        student.setUpdateTime(LocalDateTime.now());

        updateById(student);

    }

}
